<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class Member extends Model
{
    // 确定链接表名
    protected $name = 'member';

    /**
     * 根据搜索条件获取用户列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getMemberByWhere($where, $offset, $limit)
    {

        return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的用户数量
     * @param $where
     */
    public function getAllMembers($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 插入管理员信息
     * @param $param
     */
    public function insertUser($param)
    {
        try{

            $result =  $this->validate('UserValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('user/index'), '添加用户成功');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑管理员信息
     * @param $param
     */
    public function editUser($param)
    {
        try{

            $result =  $this->validate('UserValidate')->save($param, ['id' => $param['id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('user/index'), '编辑用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据管理员id获取角色信息
     * @param $id
     */
    public function getOneUser($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 删除管理员
     * @param $id
     */
    public function delUser($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除管理员成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    /**
     * 根据用户名获取管理员信息
     * @param $name
     */
    public function findUserByName($name)
    {
        return $this->where('user_name', $name)->find();
    }

    /**
     * 更新管理员状态
     * @param array $param
     */
    public function updateStatus($param = [], $uid)
    {
        try{

            $this->where('id', $uid)->update($param);
            return msg(1, '', 'ok');
        }catch (\Exception $e){

            return msg(-1, '', $e->getMessage());
        }
    }

    /**
     * 根据用户名检测用户数据
     * @param $userName
     */
    public function checkUser($userName)
    {
        return $this->alias('u')->join('role r', 'u.role_id = r.id')
            ->where('u.user_name', $userName)
            ->find();
    }

    public  function getTypeAttr($velue){
        $msg = ['默认会员组','师傅会员组','服务商'];
        return $msg[$velue];
    }

    public function getStatusAttr($velue)
    {
        $msg = ['禁用','正常'];
        return $msg[$velue];
    }
    public function getSexAttr($velue)
    {
        $msg = ['未知','男','女'];
        return $msg[$velue];
    }

    public function getShengAttr($velue)
    {
        $data = db('area')->where('id',$velue)->field('name')->find();
        return $data['name'];
    }

    public function getShiAttr($velue)
    {
        $data = db('area')->where('id',$velue)->field('name')->find();
        return $data['name'];
    }

    public function getXianAttr($velue)
    {
        $data = db('area')->where('id',$velue)->field('name')->find();
        return $data['name'];
    }

    public function getFenleiidAttr($velue)
    {
        $fenlei= db('fenlei')->where(['id'=>['in',$velue]])->field('name')->select();
        return implode('||',array_column($fenlei,'name'));
    }
    public function getImgAttr($velue)
    {
        $img1 = explode(',',$velue);
        return '<img src="'.$img1[0].'" width="40px" height="40px"><img src="'.$img1[0].'" width="40px" height="40px">';
    }

    public function getImgsAttr($velue)
    {
        $str = '';
        $img2 = array_filter(array_filter(explode(',',$velue)));
        foreach ($img2 as $k => $v){
            $str .="<img src='$v' width='40px' height='40px'>";
        }
        return $str;

    }
//
//    public function getStepAttr($velue)
//    {
//        $msg = ['','已申请','待认证','已认证'];
//        return $msg[$velue];
//    }




}
